<?php

declare(strict_types=1);

namespace App\Helper;

use App\Helper\Log;
use Hyperf\Context\Context;
use Hyperf\Utils\ApplicationContext;
use GuzzleHttp\Client;

class Util
{
    /**
     * 获取微信access_token
     * [getWxAccessToken description]
     * @return [type] [description]
     */
    public static function getWxAccessToken($appid,$appsecret){
        $container = ApplicationContext::getContainer();
        $redis = $container->get(\Redis::class);
        $redis_key = 'grabpiao_token:'.$appid;
        if ($redis->EXISTS($redis_key)) {
            return $redis->GET($redis_key);
        }
        $method = "GET";
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
        Log::get("wx_get_access_token_url")->info($url);
        $options = [];
        $response = (new Client())
            ->request($method, $url, $options)
            ->getBody()
            ->getContents();
        Log::get("wx_get_access_token_res")->info($response);
        $response = json_decode($response,true);
        if(!empty($response['access_token'])){
            $redis->SET($redis_key, $response['access_token'], $response['expires_in']);
            return $response['access_token'];
        }
        return NULL;
    }
    /**
     * 获取小程序openid
     * [getApOpenIdByCode description]
     * @param  [type] $code      [description]
     * @param  [type] $appid     [description]
     * @param  [type] $appsecret [description]
     * @return [type]            [description]
     */
    public static function getApOpenIdByCode($code,$appid,$appsecret){
        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$appsecret.'&js_code='.$code.'&grant_type=authorization_code';
        $response = file_get_contents($url);
        $response = json_decode($response, true);
        return $response;
    }
    /**
     * 获取微信openid
     * [getWxOpenIdByCode description]
     * @param  [type] $code      [description]
     * @param  [type] $appid     [description]
     * @param  [type] $appsecret [description]
     * @return [type]            [description]
     */
    public static function getWxOpenIdByCode($code,$appid,$appsecret){
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
        $response = file_get_contents($url);
        $response = json_decode($response, true);
        return $response;
    }
    /**
     * 根据公众号openid获取unionid
     * [getUnionidByWxOpenid description]
     * @param  [type] $openid    [description]
     * @param  [type] $appid     [description]
     * @param  [type] $appsecret [description]
     * @return [type]            [description]
     */
    public static function getUnionidByWxOpenid($openid,$appid,$appsecret)
    {
        $access_token = self::getWxAccessToken($appid,$appsecret);
        $url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
        Log::get("wx_get_unionid_url")->info($url);
        $response = file_get_contents($url);
        $response = json_decode($response, true);
        return $response;
    }
    /**
     * xml转数组
     * [xmlToArray description]
     * @param  [type] $xml [description]
     * @return [type]      [description]
     */
    public static function xmlToArray($xml)
    {
        $data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $data;
    }
}
